package com.twu.biblioteca.menuoptions;

import com.twu.biblioteca.LibraryManager;
import com.twu.biblioteca.TerminalPrintUtility;
import com.twu.biblioteca.TerminalReadUtility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by olga on 7/06/14.
 */
public abstract class MenuOption
{
    protected String menuTitle;
    protected boolean isQuitOption;
    protected boolean requiresLogin;

    protected String requestTitle;
    protected String titleRequestString;
    protected String creatorRequestString;
    protected String yearRequestString;

    private String title;
    private String creator;
    private String year;

    private static final String NOT_LOGGED_IN_PROMPT = "Unable to login - incorrect user credentials supplied!\nYou must be logged in to run this option.";
    private static final String USER_INPUT_ERROR = "Something went wrong - unable to read book details.";

    protected UserDetailsHandler userDetailsHandler;

    public MenuOption( String menuTitle, UserDetailsHandler userDetailsHandler )
    {
        this.menuTitle = menuTitle;

        isQuitOption = false;
        requiresLogin = false;

        requestTitle = "Please enter book details";
        titleRequestString = "Book title: ";
        creatorRequestString = "Book author: ";
        yearRequestString = "Year published: ";

        title = "";
        creator = "";
        year = "";

        this.userDetailsHandler = userDetailsHandler;
    }

    protected String getTitle() { return title; }

    protected String getCreator() {  return creator; }

    protected String getYear() {
        return year;
    }

    public String getMenuTitle() { return menuTitle; }

    public boolean isQuitOption()
    {
        return isQuitOption;
    }

    protected boolean checkUserLogin()
    {
        if( requiresLogin && userDetailsHandler != null && (userDetailsHandler.isUserLoggedIn() || userDetailsHandler.loginUser()) )
            return true;
        else
           TerminalPrintUtility.printStringWithEmptyLine(NOT_LOGGED_IN_PROMPT);
        return false;
    }

    protected void getUserInputValues()
    {
        TerminalPrintUtility.printStringWithCarriageReturn(requestTitle);
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            title = TerminalReadUtility.getUserInput(reader, titleRequestString);
            creator = TerminalReadUtility.getUserInput(reader, creatorRequestString);
            year = TerminalReadUtility.getUserInput(reader, yearRequestString);
        }
        catch( IOException e )
        {
            TerminalPrintUtility.printStringWithEmptyLine(USER_INPUT_ERROR);
        }
    }

    public abstract void action( LibraryManager libraryManager );

    protected void printSuccessOrFailure( boolean hasSucceeded, String successMsg, String failureMsg )
    {
        if( hasSucceeded )
            TerminalPrintUtility.printStringWithEmptyLine(successMsg);
        else
            TerminalPrintUtility.printStringWithEmptyLine(failureMsg);
    }
}
